package com.speljohan.rsbot.bot;

import java.io.File;

import com.speljohan.rsbot.accessors.Client;
import com.speljohan.rsbot.account.Account;
import com.speljohan.rsbot.account.AccountHandler;
import com.speljohan.rsbot.event.EventManager;
import com.speljohan.rsbot.event.events.CharacterMovedEvent;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.script.InputManager;
import com.speljohan.rsbot.script.ScriptHandler;

public class Bot {
	private static Account account;
	private static AccountHandler ah;
	private static BotStub botStub;
	private static Client client;
	private static EventManager eventManager;
	private static InputManager im;
	private static RSLoader loader;
	private static ScriptHandler sh;
	private static File workDir;

	public static void characterMoved(
			final com.speljohan.rsbot.accessors.Character c, final int i) {
		try {
			final CharacterMovedEvent e = new CharacterMovedEvent(c,i);
			Bot.eventManager.addToQueue(e);
		} catch (final Throwable e) { //protect rs
			e.printStackTrace();
		}
	}

	public static Account getAccount() {
		return Bot.account;
	}

	public static Client getClient() {
		return Bot.client;
	}

	public static EventManager getEventManager() {
		return Bot.eventManager;
	}

	public static InputManager getInputManager() {
		return Bot.im;
	}

	public static ScriptHandler getScriptHandler() {
		return Bot.sh;
	}

	public static File getWorkDir() {
		return Bot.workDir;
	}

	public static void notifyServerMessage(final String s) {
		try {
			final ServerMessageEvent e = new ServerMessageEvent(s);
			Bot.eventManager.addToQueue(e);
		} catch (final Throwable e) { //protect rs
			e.printStackTrace();
		}
	}

	// Constructor
	public Bot(final File workDir, final String world) {
		Bot.workDir = workDir;
		init(world);
	}

	public AccountHandler getAccountHandler() {
		return Bot.ah;
	}

	public BotStub getBotStub() {
		return Bot.botStub;
	}

	public RSLoader getLoader() {
		return Bot.loader;
	}

	public void init(final String world) {
		Bot.im = new InputManager();
		Bot.loader = new RSLoader();
		Bot.botStub = new BotStub(world, Bot.loader);
		Bot.loader.setStub(Bot.botStub);
		Bot.loader.setCallback(new Runnable() {
			public void run() {
				setClient((Client) Bot.loader.getClient());
			}
		});
		Bot.sh = new ScriptHandler(Bot.workDir);
		Bot.ah = new AccountHandler(Bot.workDir);
		new Thread(new Runnable() {
			public void run() {
				try {
					Bot.sh.reload();
				} finally { }
			}
		},"SRA-Load").start();
		Bot.eventManager = new EventManager();
		Bot.eventManager.start();
	}

	public void setAccount(final Account a) {
		Bot.account = a;
	}

	public void setClient(final Client cl) {
		Bot.client = cl;
		Bot.client.setCallback(new CallbackImpl(this));
	}

	public void startClient() {
		Bot.botStub.setActive(true);
		final ThreadGroup tg = new ThreadGroup("RSClient");
		final Thread thread = new Thread(tg,Bot.loader,"Loader");
		thread.start();
	}
}
